﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class CTDonHangDAO
    {
        public int ThemCTDonHang(CTDonHangDTO ctdhdto)
        {
            int res;
            SqlConnection conn = new SqlConnection();
            conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand("proc_ThemCTDonHang", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter sqlPar;

            sqlPar = new SqlParameter("@madh", SqlDbType.VarChar, 6);
            sqlPar.Direction = ParameterDirection.Input;
            sqlPar.Value = ctdhdto.MaDH;
            cmd.Parameters.Add(sqlPar);

            sqlPar = new SqlParameter("@mama", SqlDbType.VarChar, 6);
            sqlPar.Direction = ParameterDirection.Input;
            sqlPar.Value = ctdhdto.MaMA;
            cmd.Parameters.Add(sqlPar);           

            sqlPar = new SqlParameter("@soluong", SqlDbType.Int);
            sqlPar.Direction = ParameterDirection.Input;
            sqlPar.Value =ctdhdto.SoLuong;
            cmd.Parameters.Add(sqlPar);


            sqlPar = new SqlParameter("@res", SqlDbType.Int);
            sqlPar.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(sqlPar);

            res = cmd.ExecuteNonQuery();
            res = int.Parse(cmd.Parameters["@res"].Value.ToString());
            conn.Close();
            return res;

        }

        public int HuyCTDonHang(CTDonHangDTO ctdhdto)
        {
            int res;
            SqlConnection conn = new SqlConnection();
            conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand("usp_HuyDSCTDH_KH", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter sqlPar;

            sqlPar = new SqlParameter("@madh", SqlDbType.VarChar, 6);
            sqlPar.Direction = ParameterDirection.Input;
            sqlPar.Value = ctdhdto.MaDH;
            cmd.Parameters.Add(sqlPar);

            sqlPar = new SqlParameter("@res", SqlDbType.Int);
            sqlPar.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(sqlPar);

            res = cmd.ExecuteNonQuery();
            res = int.Parse(cmd.Parameters["@res"].Value.ToString());
            conn.Close();
            return res;

        }

        public List<CTDonHangDTO> LietKeCTDHTheoMaKH(string maKH)
        {
            List<CTDonHangDTO> ds = new List<CTDonHangDTO>();
            SqlConnection conn = new SqlConnection();
            conn = DataProvider.ConnectDB();

            SqlCommand cmd = new SqlCommand("usp_XemDSCTDH_KH", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter sqlPar;
            sqlPar = new SqlParameter("@makh", SqlDbType.VarChar, 6);
            sqlPar.Direction = ParameterDirection.Input;
            sqlPar.Value =maKH;
            cmd.Parameters.Add(sqlPar);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                CTDonHangDTO ct = new CTDonHangDTO();
                ct.MaDH = reader.GetString(0);
                ct.MaMA = reader.GetString(1);
               ct.SoLuong = reader.GetInt32(2);
                ds.Add(ct);
            }
            conn.Close();
            return ds;
        }
    }
}
